package org.ainlolcat.ainscience.statistics.handlers;

import org.ainlolcat.ainscience.UI.renderer.LineDataRenderer;
import org.ainlolcat.ainscience.UI.renderer.LineRendererSource;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.entity.Entity;
import org.ainlolcat.ainscience.statistics.StatisticHandler;
import org.ainlolcat.ainscience.statistics.StatisticStorage;
import org.ainlolcat.ainscience.utils.ArraysUtil;
import org.ainlolcat.ainscience.utils.XMLUtils;
import org.jdom.Element;

import javax.swing.*;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;

/**
 * @author ainlolcat
 * Date: 10/16/13
 */
public class HaaraStatisticHandler implements StatisticHandler , LineRendererSource{
    Hashtable<String, double[][]> data = new Hashtable<String, double[][]>();

    public JPanel getRenderer() {
        return new LineDataRenderer(this);
    }

    public Hashtable<String,String> getStatistic(Entity entity, Integer columnID) {
        return getStatistic(entity, columnID, null);
    }

    public Hashtable<String, String> getStatistic(Entity entity, Integer columnID, Element params) {
        Hashtable<String,String> stats = new Hashtable<String, String>();
        Column cy = entity.getColumnByOrder(columnID);
        if (cy.isNumeric()){
            Column cx = entity.getXColumn(cy.getAxis());
            if (cx.isNumeric()){
                double[] x = cx.getDoubleRepresentation();
                double[] y = cy.getDoubleRepresentation();
                if (params == null || params.getChildren("range").isEmpty()) {
                    throw new IllegalArgumentException("range cannot be empty");
                }
                else {
                    for (Element integral:(List<Element>)params.getChildren("range")){
                        //try retrieve settings from params
                        double start = XMLUtils.getSafeDouble4Attribute(integral, "start", x[0]);
                        double end = XMLUtils.getSafeDouble4Attribute(integral, "end", x[x.length-1]);
                        String name = XMLUtils.getSafeString4Attribute(integral, "name", "Haara " + start + " " + end);
                        double[][] cutted = ArraysUtil.cut(x, y, start, end);
                        data.put(name,split(cutted[0], cutted[1]));
                        Double answer = 1.0;
                        StatisticStorage.getInstance().registerHandler(entity,name,this);
                        stats.put(name, answer.toString());
                    }
                }
            }
        }
        return stats;
    }

    private double[][] split(double[] x, double[] y){
        double[][] result = new double[3][y.length/2];
        for (int i=0;i<y.length/2;i++){
            result[0][i] = (x[2*i] + x[2*i+1])/2;
            result[1][i] = (y[2*i] + y[2*i+1])/2;
            result[2][i] = (y[2*i] - y[2*i+1])/2;
        }
        return result;
    }

    public Collection<String> getNames() {
        return data.keySet();
    }

    public double[][] getData(String name) {
        return data.get(name);
    }
}
